import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Form, Input, Modal, Select, Steps, TreeSelect, Tree } from 'antd';
import DescriptionList from '@/components/DescriptionList';
const { Description } = DescriptionList;
const FormItem = Form.Item;
const { Step } = Steps;
const { TextArea } = Input;
const { Option } = Select;
const TreeNode = Tree.TreeNode;

/***
 * 文件扫描结果展示
 */
@Form.create()
@connect()
class ScanResultFormNew extends PureComponent {
  state = {};

  onLoadData = treeNode =>
    new Promise(resolve => {
      if (treeNode.props.children) {
        resolve();
        return;
      }
      const { dispatch } = this.props;
      const id = treeNode.props.eventKey;
      dispatch({
        type: 'privateFileModel/queryScanTreeData',
        payload: { id },
        callback: res => {
          treeNode.props.dataRef.children = res;
          resolve();
        },
      });
    });

  render() {
    const {
      modalVisible,
      handleModalVisible,
      scanData,
      scanRecord: { dirName, id ,fileSuffix},
    } = this.props;
    const { scanResultList } = this.state;
    const move = () => {};

    const loop = data =>
      data.map(item => {
        let title = item.dirName + ' ';
        if (item.scanChildren) {
          item.scanChildren.forEach(child => {
            title = title + "'" + child.sensitiveName + ':' + child.sensitiveNum + "次' ";
          });
        }

        let showText = title;
        if (title.length > 60) {
          showText = showText.substring(0, 60) + ' .....';
        }

        if (item.children) {
          return (
            <TreeNode
              className="ant-tree-title"
              key={item.id}
              title={<span title={title}>{showText}</span>}
              isLeaf={Boolean(!item.dirType)}
              dataRef={item}
            >
              {loop(item.children || [])}
            </TreeNode>
          );
        }
        return (
          <TreeNode key={item.id} title={title} isLeaf={Boolean(!item.dirType)} dataRef={item} />
        );
      });
    return (
      <Modal
        width={800}
        destroyOnClose
        title={'扫描结果'}
        visible={modalVisible}
        onCancel={() => handleModalVisible()}
        onOk={() => handleModalVisible()}
        maskClosable={false}
        bodyStyle={{ padding: 0, overflow: 'auto' }}
      >
        <Tree width={640} showLine={true} showIcon={true} blockNode loadData={this.onLoadData} style={{marginLeft:20}}>
          {
            ['.zip', '.rar', '.tar', '.tar.gz', '.gz'].indexOf(fileSuffix) === -1
              ?
              loop(scanData || [])
              :
              <TreeNode
                key={id}
                title={dirName}
                isLeaf={
                  ['.zip', '.rar', '.tar'].indexOf(dirName.slice(dirName.length - 4)) === -1 &&
                  ['.tar.gz'].indexOf(dirName.slice(dirName.length - 7)) === -1 &&
                  ['.gz'].indexOf(dirName.slice(dirName.length - 3)) === -1
                }
              >
                {loop(scanData || [])}
              </TreeNode>
          }
        </Tree>
      </Modal>
    );
  }
}

export default ScanResultFormNew;
